<template>
	<jk-dialog
		v-model="showDialog"
		:title="`${dataId ? '编辑' : '新增'}-工艺项目`"
		width="740px"
		:show-content-loading="showLoading"
		:confirm-btn-loading="saveBtnLoading"
		@on-cancel="onVisibleChange(false)"
		@on-visible-change="onVisibleChange"
		@on-confirm="onConfirmBtn"
	>
		<el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="110px" :show-message="false">
			<el-row ref="box1">
				<el-col :span="12">
					<el-form-item class="margin-bottom-10" label="工序" prop="processId">
						<el-select v-model="ruleForm.processId" placeholder="工序" clearable class="width-100-percent">
							<el-option v-for="item in processList" :key="item.id" :label="item.name" :value="item.id" />
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<div class="margin-bottom-4">
			<jk-button size="mini" icon="el-icon-plus" type="primary" class="item-gap" @click="onAddBtn">添加项目</jk-button>
		</div>
		<jk-table border :data="ruleForm.itemList || []" :max-height="dialogTableHeight - 30">
			<vxe-column title="操作" align="center" field="operation" :width="110">
				<template slot-scope="scope">
					<div class="flex-center">
						<jk-button size="mini" icon="el-icon-plus" class="item-gap" @click="onAddBtn(scope.rowIndex)"></jk-button>
						<jk-button size="mini" icon="el-icon-minus" @click="ruleForm.itemList.splice(scope.rowIndex, 1)"></jk-button>
					</div>
				</template>
			</vxe-column>
			<vxe-column title="序号" type="seq" align="center" :width="60"></vxe-column>
			<vxe-column title="项目名称" align="left" field="itemName" :min-width="180">
				<template slot-scope="scope">
					<jk-input v-model="scope.row.itemName" placeholder="项目名称" size="mini"></jk-input>
				</template>
			</vxe-column>
			<vxe-column title="数据类型" align="center" field="dataType" :min-width="160">
				<template slot-scope="scope">
					<el-radio-group v-model="scope.row.dataType">
						<el-radio :label="1">数字型</el-radio>
						<el-radio :label="2">字符型</el-radio>
					</el-radio-group>
				</template>
			</vxe-column>
		</jk-table>
	</jk-dialog>
</template>

<script>
    import calHeight from '/src/mixins/cal-dialog-height';
    import {
        spinSpecProjectCreate,
        spinSpecProjectUpdate,
        spinSpecProjectGet
    } from '../../../api/technology/item';
    import { spinProcessPage } from '../../../api/technology/process';
    export default {
        name: 'TechnologyItem',
        mixins: [calHeight],
        props: {
            dialogState: {
                type: Boolean,
                default: false
            },
            dataId: {
                type: [String, Number],
                default: ''
            }
        },
        data() {
            return {
                tableLoading: false,
                processList: [],
                saveBtnLoading: false,
                showDialog: false,
                showLoading: true,
                confirmBtnLoading: false,
                saveDialogState: false,
                ruleForm: {
                    'auditState': 0,
                    'itemList': [],
                    'processCode': '',
                    'processId': null,
                    'processName': '',
                    'remark': ''
                },
                rules: {
                    processId: [{ required: true, trigger: 'change' }]
                },
                defaultRowItem: {
                    'dataType': 1,
                    'id': null,
                    'itemCode': '',
                    'itemId': null,
                    'itemName': '',
                    'sortNum': null
                }
            };
        },
        methods: {
            onAddBtn(index) {
                if (index || index === 0) {
                    this.ruleForm.itemList.splice(index + 1, 0, JSON.parse(JSON.stringify(this.defaultRowItem)));
                } else {
                    this.ruleForm.itemList.push(JSON.parse(JSON.stringify(this.defaultRowItem)));
                }
            },
            onRemoveBtn(scope) {
                if (this.ruleForm.itemList.length > 1) {
                    this.ruleForm.itemList.splice(scope.rowIndex, 1);
                }
            },
            getParamData() {
                const params = JSON.parse(JSON.stringify(this.ruleForm));
                if (!this.dataId) delete params.id;
                const processItem = this.processList.find(x => x.id === params.processId) || {};
                params.processCode = processItem.code;
                params.processName = processItem.name;
                return params;
            },
            onConfirmBtn() {
                this.$refs['ruleForm'].validate(valid => {
                    const isPass = this.ruleForm.itemList.every(x => x.itemName);
                    if (valid && isPass) {
                        this.createAndUpdateRequest();
                    } else {
                        this.$tipsBar(this, 'validator');
                    }
                });
            },
            createAndUpdateRequest() {
                this.saveBtnLoading = true;
                const api = !this.dataId ? spinSpecProjectCreate : spinSpecProjectUpdate;
                api(this.getParamData()).then(res => {
                    this.saveBtnLoading = false;
                    if (res.data.status === 200) {
                        this.$tipsBar(this, 'save');
                        this.$emit('on-finish');
                    }
                });
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                    this.$nextTick(() => {
                        this.$refs.ruleForm.resetFields();
                    });
                }
            },
            getDetailRequest() {
                return spinSpecProjectGet({ id: this.dataId }).then(res => {
                    if (res.data.status === 200) {
                        this.ruleForm = res.data.res;
                    }
                });
            },
            getProcessListRequest() {
                return spinProcessPage().then(res => {
                    if (res.data.status === 200) {
                        this.processList = res.data.res || [];
                    }
                });
            },
            async getDependentData() {
                this.showLoading = true;
                await this.getProcessListRequest();
                if (this.dataId) await this.getDetailRequest();
                this.showLoading = false;
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal) {
                    this.getDependentData();
                }
            }
        }
    };
</script>
<style scoped lang="scss">
.menu-title-icon {
    font-size: 29px;
    color: #909399;
    margin-right: 4px;
}
.edit-tab-icon {
    margin-left: 8px;
    font-size: 13px;
}
.edit-tab-icon:hover {
    cursor: pointer;
}
.drag-info-icon {
    font-size: 14px;
}
.drag-btn {
    cursor: move;
    color: #2d8cf0;
    transition: all .5s;
    font-size: 18px;
}
.drag-btn:hover {
    transform: rotate(90deg);
}
</style>
